package com.jinzecloud.reportapproval.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinzecloud.reportapproval.bean.SafetyAssessment;
import com.jinzecloud.reportapproval.dto.AuditParam;
import com.jinzecloud.reportapproval.dto.DataStagingParam;
import com.jinzecloud.reportapproval.enums.StateEnum;
import com.jinzecloud.reportapproval.service.AdminService;
import com.jinzecloud.reportapproval.service.SafetyAssessmentService;
import com.jinzecloud.reportapproval.utils.R;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;

@RestController
@RequestMapping("/safetyAssessment")
@RequiredArgsConstructor
public class SafetyAssessmentController {

    private final SafetyAssessmentService safetyAssessmentService;
    private final AdminService adminService;

    @PostMapping
    public R save(@RequestBody SafetyAssessment safetyAssessment) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        safetyAssessment.setState(StateEnum.UN_SUBMIT.getState()).setWordPath("").setPdfPath("");
        safetyAssessment.setCreatedUserId(adminService.getIdByName(authentication.getName()));
        R r = safetyAssessmentService.saveSafetyAssessment(safetyAssessment);
        return r;
    }

    @PutMapping
    public R update(@RequestBody SafetyAssessment safetyAssessment) {
        safetyAssessment.setState(StateEnum.UN_SUBMIT.getState()).setWordPath("").setPdfPath("");
        safetyAssessmentService.updateById(safetyAssessment);
        return R.ok();
    }

    @PostMapping("/review")
    public R review(@RequestBody SafetyAssessment safetyAssessment) {
        safetyAssessmentService.updateById(new SafetyAssessment().setId(safetyAssessment.getId()).setState(0));
        return R.ok();
    }

    @PostMapping("/audit")
    public R audit(@RequestBody AuditParam auditParam) {
        return safetyAssessmentService.audit(auditParam);
    }

    @PostMapping("/list")
    public R list(@RequestBody DataStagingParam param) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return R.ok().setData(safetyAssessmentService.page(new Page<>(param.getPageNo(), param.getPageSize()),
                new LambdaQueryWrapper<SafetyAssessment>().and(w -> w.ne(SafetyAssessment::getState, StateEnum.APPROVAL_PASS.getState()).eq(SafetyAssessment::getCreatedUserId, adminService.getIdByName(authentication.getName())
                        ).or().eq(SafetyAssessment::getState, StateEnum.APPROVAL_PASS.getState()))
                        .and(w -> w.like(SafetyAssessment::getProjectCode, param.getSearchKey()))
        ));
    }

    @PostMapping("/auditList")
    public R auditList(@RequestBody DataStagingParam param) {
        return R.ok().setData(safetyAssessmentService.auditList(param));
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        return R.ok().setData(safetyAssessmentService.getById(id));
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        safetyAssessmentService.removeById(id);
        return R.ok();
    }

    @PostMapping("/batchDelete")
    public R batchDelete(@RequestBody Long[] ids) {
        safetyAssessmentService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @GetMapping("/getFilePath/{id}")
    public R getReportFilePath(@PathVariable("id") String id, HttpServletRequest request) throws IOException, IllegalAccessException {
        String path = safetyAssessmentService.getReportFilePath(id, request);
        return R.ok().put("path", path);
    }

    @RequestMapping("/viewPdf/{id}")
    public String viewPdf(@PathVariable("id") Integer id) {
        return safetyAssessmentService.viewPdf(id);
    }

    @RequestMapping("/viewWord/{id}")
    public String viewWord(@PathVariable("id") Integer id) {
        return safetyAssessmentService.viewWord(id);
    }

}
